package mortal.game.control
{
    import Message.Game.*;
    import com.gengine.utils.*;
    import flash.events.*;
    import modules.*;
    import mortal.common.sound.*;
    import mortal.component.window.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.*;
    import mortal.game.view.shopMall.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class ShopMallController extends Controller
    {
        private var _shopMallModule:ShopMallModule;
        private var _previewWindow:PreviewWindow;

        public function ShopMallController()
        {
            Dispatcher.addEventListener(EventName.ShopMallBuyItem, this.buyItemHandler);
            Dispatcher.addEventListener(EventName.WareHouseBuyItem, this.buyItemHandler);
            Dispatcher.addEventListener(EventName.ShopMallBuyItemAndUse, this.buyItemAndUseHandler);
            Dispatcher.addEventListener(EventName.ShopMallBuyItemSuccess, this.onShopMallBuyItemSuccess);
            Dispatcher.addEventListener(EventName.PanicBuyItem, this.onPanicBuyItemHandler);
            Dispatcher.addEventListener(EventName.GetPanicBuyConfig, this.onGetPanicBuyConfigHandler);
            Dispatcher.addEventListener(EventName.GetPanicBuyConfigSuccess, this.onGetPanicBuyConfigSuccessHandler);
            Dispatcher.addEventListener(EventName.ClosePanicBuy, this.onClosePanicBuyHandler);
            Dispatcher.addEventListener(EventName.PanicBuyItemUpdate, this.onPanicBuyItemUpdateHandler);
            Dispatcher.addEventListener(EventName.PanicBuyRefresh, this.onPanicBuyRefreshHandler);
            Dispatcher.addEventListener(EventName.GuidOpenShopMall, this.onGuidOpenShopMallHandler);
            Dispatcher.addEventListener(EventName.ShopMallOpen, this.onShopMallOpenHandler);
            Dispatcher.addEventListener(EventName.PetMountPreview, this.onPetMountPreviewHandler);
            Dispatcher.addEventListener(EventName.VIPOpenShopMall, this.onVIPOpenShopMallHandler);
            Dispatcher.addEventListener(EventName.ShopMallShowToTab, this.onShopMallShowToTabHandler);
            
        }

        override public function get view() : IView
        {
            return this.initView();
        }

        override protected function initView() : IView
        {
            if (this._shopMallModule == null)
            {
                this._shopMallModule = new ShopMallModule();
                this._shopMallModule.addEventListener(WindowEvent.SHOW, this.onAddedToStageHandler);
            }
            return this._shopMallModule;
        }

        private function onAddedToStageHandler(event:Event) : void
        {
            this._shopMallModule.showShopByIndex(0);
            this.onMoneyUpdateHandler();
            Dispatcher.dispatchEvent(new DataEvent(EventName.GetPanicBuyConfig));
            
        }

        override protected function initServer() : void
        {
            NetDispatcher.addCmdListener(ServerCommand.MoneyUpdate, this.onMoneyUpdateHandler);
            
        }

        private function onMoneyUpdateHandler(param1:SMoney = null) : void
        {
            if (this._shopMallModule && this._shopMallModule.isHide == false)
            {
                this._shopMallModule.update(cache.role.money);
            }
            
        }

        private function buyItemHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data["shopItemInfo"] as ShopItemInfo;
            var _loc_3:* = event.data["amount"] as int;
            GameProxy.shopProxy.buyItem(0, _loc_2.tshopSell.shopCode, _loc_2.itemInfo.item.code, _loc_3);
            
        }

        private function buyItemAndUseHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data["shopItemInfo"] as ShopItemInfo;
            var _loc_3:* = event.data["amount"] as int;
            GameProxy.shopProxy.buyItemAndUse(0, _loc_2.tshopSell.shopCode, _loc_2.itemInfo.item.code, _loc_3);
            
        }

        private function onPanicBuyItemHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as int;
            GameProxy.shopProxy.panicBuyItem(_loc_2);
            
        }

        private function onGetPanicBuyConfigHandler(event:DataEvent) : void
        {
            GameProxy.shopProxy.getPanicBuyConfig();
            
        }

        private function onGetPanicBuyConfigSuccessHandler(event:DataEvent) : void
        {
            this._shopMallModule.updatePreferenPanel(cache.shop.refreshMsg);
            
        }

        private function onPanicBuyItemUpdateHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SPanicBuyItemMsg;
            this._shopMallModule.refreshDataProvider(_loc_2);
            
        }

        private function onPanicBuyRefreshHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SPanicBuyRefreshMsg;
            this._shopMallModule.refreshDataProvider(_loc_2);
            
        }

        private function onClosePanicBuyHandler(event:DataEvent) : void
        {
            GameProxy.shopProxy.closePanicBuyPanel();
            
        }

        private function onShopMallBuyItemSuccess(event:DataEvent) : void
        {
            var _loc_2:String = "";
            var _loc_3:* = event.data["itemCode"] as int;
            var _loc_4:* = event.data["amount"] as int;
            var _loc_5:* = ItemConfig.instance.getInfoByCode(_loc_3);
            if (ItemConfig.instance.getInfoByCode(_loc_3) && _loc_5.item)
            {
                _loc_2 = HTMLUtil.addColor(_loc_5.item.name, ColorConfig.instance.getItemColor(_loc_5.item.color).color);
            }
            MsgManager.addBroadCast("成功购买了" + _loc_4 + "个" + _loc_2);
            SoundManager.instance.soundPlay(SoundTypeConst.GoodsTrade);
            
        }

        private function onPetMountPreviewHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data["type"] as int;
            var _loc_3:* = event.data["code"] as int;
            if (this._previewWindow == null)
            {
                this._previewWindow = new PreviewWindow();
            }
            this._previewWindow.clear();
            this._previewWindow.updatePetModel(_loc_3, _loc_2);
            this._previewWindow.show();
            
        }

        private function onShopMallOpenHandler(event:DataEvent) : void
        {
            this.view.show();
            
        }

        public function onGuidOpenShopMallHandler(event:DataEvent) : void
        {
            this.onShopMallOpenHandler(null);
            this._shopMallModule.showGuideTab();
            
        }

        private function onVIPOpenShopMallHandler(event:DataEvent) : void
        {
            if (cache.vip.isVIP)
            {
                this.onShopMallOpenHandler(null);
                this._shopMallModule.showShopByIndex(7);
            }
            
        }

        private function onShopMallShowToTabHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as int;
            this.onShopMallOpenHandler(null);
            this._shopMallModule.showShopByIndex(_loc_2);
            
        }

    }
}
